package com.basic.business.service.impl;

import com.basic.business.common.shiro.CustomerToken;
import com.basic.business.common.shiro.JwtEntity;
import com.basic.business.common.shiro.JwtUtils;
import com.basic.business.common.shiro.LoginToken;
import com.basic.business.mapper.SysUserMapper;
import com.basic.business.model.dto.SysUserDto;
import com.basic.business.model.dto.SysUserLoginDto;
import com.basic.business.model.entity.SysUser;
import com.basic.business.model.vo.SysUserVo;
import com.basic.business.service.LoginService;
import com.basic.business.utils.AESUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.alibaba.fastjson.JSON;


/**
 * @author ：huage
 * @date ：Created in 2021/7/20 17:12
 * @description：登录service
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private SysUserMapper sysUserMapper;


    @Override
    public SysUserDto getUserByName(String username) {
        return null;
    }

    @Override
    public SysUserLoginDto login(SysUserVo user) {
        LoginToken usernamePasswordToken = new LoginToken(user.getName(), AESUtil.encodeByAES(user.getPassword()));
        Subject subject = SecurityUtils.getSubject();
        //登录操作
        subject.login(usernamePasswordToken);
        //返回前端token信息
        JwtEntity jwtEntity = (JwtEntity) subject.getPrincipal();
        return new SysUserLoginDto(JwtUtils.createJwtToken(JSON.toJSONString(jwtEntity)), jwtEntity.getLoginId());
    }

    @Override
    public SysUser getUserByNameAndPwd(String name, String password) {
        return sysUserMapper.getUserByNameAndPwd(name,password);
    }

}
